<template>
  <div>
    <el-dialog
      title="新增"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
      :before-close="handleClose"
    >
      <div>
        <el-form
          :label-position="labelPosition"
          label-width="80px"
          :model="formLabelAlign"
        >
          <el-form-item label="名称">
            <el-input v-model="formLabelAlign.tagName"></el-input>
          </el-form-item>
          <el-form-item label="code值" v-if="flagCode"
              prop="tagCode"
              :rules="[
                {
                  required: true,
                  message: 'code值为英文和数字',
                  trigger: 'blur',
                },
                { validator: validateCode, trigger: 'blur' },
              ]"
          >
            <el-input v-model="formLabelAlign.tagCode" ></el-input>
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="formLabelAlign.tagSort"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmitTagDefine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { tagDefineEdit } from "@/api/students/organization/consult";
import store from "@/store";
export default {
  data() {
    return {
      dialogVisible: false,
      labelPosition: "right",
      formLabelAlign: {
        tagName: "",
        tagCode: "",
        tagSort: "",
      },
      tagType: "",
      groupData:null,
      categories:null,
      flagCode:false,
      selectedGroupIndex:0,
      selectedCategoryIndex:0,
    };
  },
  methods: {
    init(type,groupData,categories,selectedGroupIndex,selectedCategoryIndex) {
      this.clear();
      this.dialogVisible = true;
      this.tagType = type;
      this.categories = categories;
      this.groupData = groupData;
      this.selectedGroupIndex = selectedGroupIndex;
      this.selectedCategoryIndex = selectedCategoryIndex;
      if(type !== 3){
        this.flagCode = false;
      }else{
        this.flagCode = true;
      }
    },
    // code值
    validateCode(rule, value, callback) {
      if (!/^[a-zA-Z0-9]*$/.test(value)) {
        callback(new Error("请输入正确的code值"));
      } else {
        // 校验通过时手动调用callback()来清除错误提示
        callback();
      }
    },
    onSubmitTagDefine() {
      const etype = { 
        type: this.tagType,
        data: this.groupData
      }
      if (this.tagType === 1) {
        const params = {
          parentId: "-1",
          userId: store.getters.user_id.toString(),
          token: store.getters.token.toString(),
          tagName: this.formLabelAlign.tagName,
          tagSort: this.formLabelAlign.tagSort,
          tagType: this.tagType,
        };
        tagDefineEdit(params).then((result) => {
          this.dialogVisible = false;
          this.$emit("initData");
          // 这里就是添加 或者编辑成功 
          // this.$emit('on-group-add-success', etype)
        });
      }else if(this.tagType === 2){
        const params = {
          parentId: this.groupData.id,
          userId: store.getters.user_id.toString(),
          token: store.getters.token.toString(),
          tagName: this.formLabelAlign.tagName,
          tagSort: this.formLabelAlign.tagSort,
          tagType: this.tagType,
        };
        tagDefineEdit(params).then((result) => {
          this.dialogVisible = false;
          // this.$emit("initData");
          this.$emit('on-group-add-success', etype)
        });
      }else if(this.tagType === 3){
        const params = {
          parentId: this.categories[this.selectedCategoryIndex].id,
          tagGroupId:this.categories[this.selectedCategoryIndex].parentId,
          userId: store.getters.user_id.toString(),
          token: store.getters.token.toString(),
          tagName: this.formLabelAlign.tagName,
          tagSort: this.formLabelAlign.tagSort,
          tagType: this.tagType,
          tagCode:this.formLabelAlign.tagCode,
        };
        tagDefineEdit(params).then((result) => {
          this.dialogVisible = false;
          // this.$emit("initData");
          this.$emit('on-group-add-success', etype)
        });
      }
    },
    handleClose() {
      this.dialogVisible = false;
    },
    clear(){
      this.formLabelAlign.tagName = "";
      this.formLabelAlign.tagCode = "";
      this.formLabelAlign.tagSort = "";
    }
  },
};
</script>

<style scoped>
::v-deep .el-dialog {
  margin-top: 15vh !important;
}
</style>